package com.base.cn.platform.os.service.user.account;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.user.account.CusUserCashAccountDao;
import com.base.cn.platform.os.entity.user.account.CusUserCashAccount;
import com.base.cn.platform.os.entity.user.account.CusUserCashAccountCondition;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户提现账户Biz
 *
 * @author s.li
 * @date 2018/12/01
 */
@Service
public class CusUserCashAccountBiz extends BaseBiz<CusUserCashAccount, CusUserCashAccountDao> {

    /***
     * 通过条件，查询提现账户对象
     * @param condition 查询条件
     * @return 提现账户对象
     */
    public CusUserCashAccount findUserCashAccount(CusUserCashAccountCondition condition){
        String whereSql = this.getWhereSql(condition);
        return this.findOne(whereSql,null);
    }

    /**
     * 保存用户提现账户
     * @param userCashAccount 提现账户对象
     */
    public void saveUserCashAccount(CusUserCashAccount userCashAccount){
        CusUserCashAccount _userCashAccount = this.findOne(" userId = "+userCashAccount.getUserId()+" and accountType = "+userCashAccount.getAccountType(),null);
        if(ObjectUtils.isNotEmpty(_userCashAccount)){//更新
            _userCashAccount.setAccount(userCashAccount.getAccount());
            _userCashAccount.setRealName(userCashAccount.getRealName());
            this.updateById(_userCashAccount,null);
        }else{//添加
            this.save(userCashAccount);
        }
    }

    /**
     * 查询用户的账户列表
     * @param userId 用户ID
     * @return 提现账户列表
     */
    public Map<Integer,CusUserCashAccount> findUserCashAccount(BigDecimal userId){
        List<CusUserCashAccount> userCashAccountList = this.find(" userId ="+userId,null,null);
        if(ObjectUtils.isNotEmpty(userCashAccountList)){
            return userCashAccountList.stream().collect(Collectors.toMap(e->e.getAccountType(),e->e));
        }
        return new HashMap<>();
    }

    /**
     * 获取查询条件
     * @param condition
     * @return
     */
    private String getWhereSql(CusUserCashAccountCondition condition){
        StringBuilder whereSql = new StringBuilder(" 1=1");
        if(ObjectUtils.isNotEmpty(condition)){
            if(DataUtil.idIsNotNull(condition.getUserId())){
                whereSql.append(" and userId =").append(condition.getUserId());
            }
            if(ObjectUtils.isNotEmpty(condition.getAccountType())){
                whereSql.append(" and accountType =").append(condition.getAccountType());
            }
            if(StringUtils.isNotEmpty(condition.getAccount())){
                whereSql.append(" and account = ").append("'").append(condition.getAccount()).append("'");
            }
        }
        return whereSql.toString();
    }
}
